package com.xcl.controller;

import com.xcl.pojo.Category;
import com.xcl.pojo.Result;
import com.xcl.service.CategoryService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")//避免请求同名冲突
//@CrossOrigin //允许跨域访问
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll(){
        List<Category> lst = categoryService.findAll();
        return Result.success(lst);
    }

    //删除
    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        Integer num = categoryService.deleteById(id);
        if(num > 0){
            return Result.success();
        }
        return Result.error("删除失败");
    }

    //新增分类
    @RequestMapping("/add")
    public Result addCategory(@Valid @RequestBody Category category){
        Integer num = categoryService.addCategory(category);
        if(num > 0){
            return Result.success();
        }
        return Result.error("新增失败");
    }

    //修改数据
    @RequestMapping("/update")
    public Result update(@Valid @RequestBody Category category){
        Integer num = categoryService.update(category);
        if(num > 0){
            return Result.success();
        }
        return Result.error("修改失败");
    }
}
